package com.yunti.business.utils;

import com.ruoyi.common.annotation.Excel;
import com.yunti.business.domain.BaseStudent;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.prefs.Preferences;

import com.spire.doc.*;
import com.spire.doc.documents.BorderStyle;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.VerticalAlignment;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;

import javax.servlet.http.HttpServletResponse;


public class WordUtil<T> {
    /**
     * 导入导出数据列表
     */
    private List<T> list;

    /**
     * 实体对象
     */
    public Class<T> clazz;
    private String title;
    private String secondTitle;

    public WordUtil(Class<T> clazz) {
        this.clazz = clazz;
    }

    public void init(List<T> list, String sheetName, String title, Excel.Type type) {
        if (list == null) {
            list = new ArrayList<T>();
        }
        this.list = list;
        this.title = title;
    }

    public void setSecondTitle(String secondTitle) {
        this.secondTitle = secondTitle;
    }

    public static void output(HttpServletResponse response,List<BaseStudent> baseStudentList,String title,String secondTitle) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");

        //创建一个 Document 对象
        com.spire.doc.Document document = new Document();

        //添加一个节
        Section section = document.addSection();

        //添加表格
        Table headerTable = section.addTable(false);
        headerTable.resetCells(1, 1);

        //列宽自动适应内容
        headerTable.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);

        //添加内容到单元格并设置字体
        TextRange tittlTextRange = headerTable.get(0, 0).addParagraph().appendText(title);
        tittlTextRange.getCharacterFormat().setFontName("宋体");
        tittlTextRange.getCharacterFormat().setFontSize(18);
        tittlTextRange.getCharacterFormat().setBold(true);
        headerTable.getRows().get(0).getCells().get(0).getParagraphs().get(0).getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        headerTable.getRows().get(0).getCells().get(0).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
        headerTable.getRows().get(0).getCells().get(0).setWidth(0.5f);


        if (false) {
            //添加图片到单元格，并自定义图片大小
            DocPicture picture = headerTable.getRows().get(0).getCells().get(0).addParagraph().appendPicture("resources\\logo_left.png");//添加图片到单元格（0，0）
            picture.setWidth(48f);//设置图片宽度
            picture.setHeight(48f);//设置图片高度
            headerTable.getRows().get(0).getCells().get(0).getParagraphs().get(0).getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

            //添加图片到单元格，并自定义图片大小
            DocPicture picture2 = headerTable.getRows().get(0).getCells().get(2).addParagraph().appendPicture("resources\\logo_right.png");//添加图片到单元格（0，0）
            picture2.setWidth(48f);//设置图片宽度
            picture2.setHeight(48f);//设置图片高度
            headerTable.getRows().get(0).getCells().get(2).getParagraphs().get(0).getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        }

        Paragraph secTittleParagraph = section.addParagraph();
        TextRange secTitlTextRange = secTittleParagraph.appendText("抽签结果表");
        secTitlTextRange.getCharacterFormat().setFontSize(14);
        secTitlTextRange.getCharacterFormat().setBold(true);
        secTittleParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        secTittleParagraph.getFormat().setBeforeSpacing(10);
        secTittleParagraph.getFormat().setAfterSpacing(10);

        Paragraph gameNameParagraph = section.addParagraph();
        TextRange gameNameTextRange = gameNameParagraph.appendText("\n学校:"+secondTitle + "\n");
        gameNameTextRange.getCharacterFormat().setFontSize(14);
        gameNameTextRange.getCharacterFormat().setBold(true);
        gameNameParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
        gameNameParagraph.getFormat().setBeforeSpacing(10);
        gameNameParagraph.getFormat().setAfterSpacing(10);

        //在页脚中添加一个段落
        Paragraph gameDateParagraph = section.addParagraph();
        TextRange gameDateTextRange = gameNameParagraph.appendText("抽签时间："+sdf.format(new Date()));
        gameDateTextRange.getCharacterFormat().setFontSize(14);
        gameDateTextRange.getCharacterFormat().setBold(true);
        gameDateParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
        gameDateParagraph.getFormat().setBeforeSpacing(10);
        gameDateParagraph.getFormat().setAfterSpacing(10);

//        Paragraph gameTimeParagraph = section.addParagraph();
//        TextRange gameTimeTextRange = gameTimeParagraph.appendText("比赛时间: ");
//        gameTimeTextRange.getCharacterFormat().setFontSize(10);
//        gameTimeTextRange.getCharacterFormat().setBold(true);
//        gameTimeParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
//        gameTimeParagraph.getFormat().setBeforeSpacing(12);
//        gameTimeParagraph.getFormat().setAfterSpacing(4);
//
//        Paragraph gamePositionParagraph = section.addParagraph();
//        TextRange gamePositionTextRange = gamePositionParagraph.appendText("比赛地点: ");
//        gamePositionTextRange.getCharacterFormat().setFontSize(10);
//        gamePositionTextRange.getCharacterFormat().setBold(true);
//        gamePositionParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
//        gamePositionParagraph.getFormat().setBeforeSpacing(4);
//        gamePositionParagraph.getFormat().setAfterSpacing(4);

        //添加表格
        Table gameTable = section.addTable(false);
        gameTable.resetCells(baseStudentList.size() + 1, 7);
        //列宽自动适应内容
        gameTable.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);

        gameTable.getRows().get(0).getCells().get(0).setWidth(200);
        gameTable.getRows().get(0).getCells().get(1).setWidth(200);
        gameTable.getRows().get(0).getCells().get(2).setWidth(200);
        gameTable.getRows().get(0).getCells().get(3).setWidth(200);
        gameTable.getRows().get(0).getCells().get(4).setWidth(200);

        //定义表格数据
        String[] tableHeader = {"序号","班级","学号", "民族", "姓名", "性别", "身份证号"};
        gameTable.getRows().get(0).setHeight(24);
        for (int i = 0; i < tableHeader.length; i++) {
            gameTable.getRows().get(0).getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);

            //设置表格的右边框
            gameTable.getRows().get(0).getCells().get(i).getCellFormat().getBorders().getRight().setBorderType(BorderStyle.Hairline);
            gameTable.getRows().get(0).getCells().get(i).getCellFormat().getBorders().getRight().setLineWidth(1.0F);
            gameTable.getRows().get(0).getCells().get(i).getCellFormat().getBorders().getRight().setColor(Color.BLACK);

            gameTable.getRows().get(0).getCells().get(i).getCellFormat().getBorders().getTop().setBorderType(BorderStyle.Hairline);
            gameTable.getRows().get(0).getCells().get(i).getCellFormat().getBorders().getTop().setLineWidth(1.0F);
            gameTable.getRows().get(0).getCells().get(i).getCellFormat().getBorders().getTop().setColor(Color.BLACK);

            gameTable.getRows().get(0).getCells().get(i).getCellFormat().getBorders().getBottom().setBorderType(BorderStyle.Hairline);
            gameTable.getRows().get(0).getCells().get(i).getCellFormat().getBorders().getBottom().setLineWidth(1.0F);
            gameTable.getRows().get(0).getCells().get(i).getCellFormat().getBorders().getBottom().setColor(Color.BLACK);

            gameTable.getRows().get(0).getCells().get(i).getCellFormat().getBorders().getLeft().setBorderType(BorderStyle.Hairline);
            gameTable.getRows().get(0).getCells().get(i).getCellFormat().getBorders().getLeft().setLineWidth(1.0F);
            gameTable.getRows().get(0).getCells().get(i).getCellFormat().getBorders().getLeft().setColor(Color.BLACK);

            Paragraph p = gameTable.getRows().get(0).getCells().get(i).addParagraph();
            p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            TextRange txtRange = p.appendText(tableHeader[i]);
            txtRange.getCharacterFormat().setBold(true);
        }

        for (int i = 0; i < baseStudentList.size(); i++) {
            gameTable.getRows().get(i + 1).setHeight(24);

            Paragraph p = gameTable.getRows().get(i + 1).getCells().get(0).addParagraph();
            gameTable.getRows().get(i + 1).getCells().get(0).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
            p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            TextRange txtRange = p.appendText(baseStudentList.get(i).getNo() == null ? "" : baseStudentList.get(i).getNo() + "");
            txtRange.getCharacterFormat().setBold(true);

            p = gameTable.getRows().get(i + 1).getCells().get(1).addParagraph();
            gameTable.getRows().get(i + 1).getCells().get(1).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
            p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            txtRange = p.appendText(baseStudentList.get(i).getClassName() == null ? "" : baseStudentList.get(i).getClassName() + "");
            txtRange.getCharacterFormat().setBold(true);

            p = gameTable.getRows().get(i + 1).getCells().get(2).addParagraph();
            gameTable.getRows().get(i + 1).getCells().get(2).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
            p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            txtRange = p.appendText(baseStudentList.get(i).getStuNo() == null ? "" : baseStudentList.get(i).getStuNo() + "");
            txtRange.getCharacterFormat().setBold(true);

            p = gameTable.getRows().get(i + 1).getCells().get(3).addParagraph();
            gameTable.getRows().get(i + 1).getCells().get(3).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
            p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            txtRange = p.appendText(baseStudentList.get(i).getNation() == null ? "" : baseStudentList.get(i).getNation() + "");
            txtRange.getCharacterFormat().setBold(true);


            p = gameTable.getRows().get(i + 1).getCells().get(4).addParagraph();
            gameTable.getRows().get(i + 1).getCells().get(4).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
            p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            txtRange = p.appendText(baseStudentList.get(i).getName() == null ? "" : baseStudentList.get(i).getName() + "");
            txtRange.getCharacterFormat().setBold(true);

            p = gameTable.getRows().get(i + 1).getCells().get(5).addParagraph();
            gameTable.getRows().get(i + 1).getCells().get(5).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
            p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

            if(baseStudentList.get(i).getSex() == null ){
                txtRange = p.appendText( "");
            }else{
                if(baseStudentList.get(i).getSex() ==0){
                    txtRange = p.appendText("男");
                }else{
                    txtRange = p.appendText("女");
                }

            }
            txtRange.getCharacterFormat().setBold(true);

            p = gameTable.getRows().get(i + 1).getCells().get(6).addParagraph();
            gameTable.getRows().get(i + 1).getCells().get(6).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
            p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            txtRange = p.appendText(baseStudentList.get(i).getCardId());
            txtRange.getCharacterFormat().setBold(true);


            for (int j = 0; j <= 6; j++) {
                gameTable.getRows().get(i + 1).getCells().get(j).getCellFormat().getBorders().getTop().setBorderType(BorderStyle.Hairline);
                gameTable.getRows().get(i + 1).getCells().get(j).getCellFormat().getBorders().getTop().setLineWidth(1.0F);
                gameTable.getRows().get(i + 1).getCells().get(j).getCellFormat().getBorders().getTop().setColor(Color.BLACK);
                //设置表格的右边框
                gameTable.getRows().get(i + 1).getCells().get(j).getCellFormat().getBorders().getBottom().setBorderType(BorderStyle.Hairline);
                gameTable.getRows().get(i + 1).getCells().get(j).getCellFormat().getBorders().getBottom().setLineWidth(1.0F);
                gameTable.getRows().get(i + 1).getCells().get(j).getCellFormat().getBorders().getBottom().setColor(Color.BLACK);

                gameTable.getRows().get(i + 1).getCells().get(j).getCellFormat().getBorders().getLeft().setBorderType(BorderStyle.Hairline);
                gameTable.getRows().get(i + 1).getCells().get(j).getCellFormat().getBorders().getLeft().setLineWidth(1.0F);
                gameTable.getRows().get(i + 1).getCells().get(j).getCellFormat().getBorders().getLeft().setColor(Color.BLACK);

                gameTable.getRows().get(i + 1).getCells().get(j).getCellFormat().getBorders().getRight().setBorderType(BorderStyle.Hairline);
                gameTable.getRows().get(i + 1).getCells().get(j).getCellFormat().getBorders().getRight().setLineWidth(1.0F);
                gameTable.getRows().get(i + 1).getCells().get(j).getCellFormat().getBorders().getRight().setColor(Color.BLACK);
            }
        }


        HeaderFooter footer = section.getHeadersFooters().getFooter();
        //在页脚中添加一个段落
        Paragraph qianminParagraph = section.addParagraph();
        //添加 Field_Page 和 Field_Num_Pages 域到页脚段落
        qianminParagraph.appendText("学校代表签字：       ");

        qianminParagraph.getFormat().setBeforeSpacing(20);
        qianminParagraph.getFormat().setAfterSpacing(20);
        qianminParagraph.getFormat().setRightIndent(40);
        qianminParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);

        qianminParagraph = section.addParagraph();
        //添加 Field_Page 和 Field_Num_Pages 域到页脚段落
        qianminParagraph.appendText("云南省学校体育教学指导委员会：       ");

        qianminParagraph.getFormat().setBeforeSpacing(20);
        qianminParagraph.getFormat().setAfterSpacing(50);
        qianminParagraph.getFormat().setRightIndent(40);
        qianminParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);



        //保存文件
        try {
            document.saveToStream(response.getOutputStream(), FileFormat.Docx_2013);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
